<?php

namespace app\manystore\controller\salescenter;

use app\common\controller\ManystoreBase;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

use think\Db;
use app\manystore\Model\UserModel;
use app\manystore\Model\CampusClewConsultantModel;
use app\manystore\Model\ManystoreModel;
use app\manystore\Model\CourseModel;
use app\manystore\Model\TypeModel;
use app\manystore\Model\CampusClass;


/**
 * 数据源接口
 */
class DataSource extends ManystoreBase
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $auth = [];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取指定列表
     *
     * @ApiTitle    获取指定列表
     * @ApiMethod   (GET)

     * @ApiParams (name="consultant", type="integer", required=false, description="为1时获取课程顾问")
     * @ApiParams (name="collector", type="integer", required=false, description="为1时获取采单人列表")
     * @ApiParams (name="distribution", type="integer", required=false, description="为1时获取可分配跟进人列表【服务商可分配校区，校区可分配老师】")
     * @ApiParams (name="students_source", type="integer", required=false, description="为1时获取学员来源列表")
     * @ApiParams (name="label", type="integer", required=false, description="为1时获取标签列表")
     * @ApiParams (name="delete_why", type="integer",required=false,description="为1时获取删除原因列表")
     * @ApiParams (name="growth", type="integer",required=false,description="为1时获取档案类型列表")
     * @ApiParams (name="student", type="integer",required=false,description="为1时获取可操作线索列表")
     * @ApiParams (name="teacher", type="integer",required=false,description="为1时获取老师列表")
     * @ApiParams (name="course", type="integer",required=false,description="为1时获取课程列表")
     * @ApiParams (name="class", type="integer",required=false,description="为1时获取本校区所有班级列表,2获取1对1班级,3获取1对多")
     * @ApiParams (name="clues", type="integer",required=false,description="为1时获取意向级别")
     * @ApiParams (name="followway", type="integer",required=false,description="为1时获取跟进阶段")
     * @ApiParams (name="followstage", type="integer",required=false,description="为1时获取跟进阶段")
     * @ApiParams (name="goodsubjects", type="integer",required=false,description="为1时获取擅长科目")
     * @ApiParams (name="quickentry", type="integer",required=false,description="为1时获取快捷入口")
     * 
     * @ApiSector   销售中心
     * @ApiReturn   ({
        'code' : '1',
        'msg' : '获取数据成功',
        'time' : '1657378506',
        'data' : {
              "collector": [{id:1,name:'课程顾问'}],
              "students_source": [{id:1,name:'学员来源'}],
              ......
            }
     * })
     */
    public function getData()
    {
        $param = $this->request->param();
        $data = [];

        if(!empty($param['consultant']) && $param['consultant'] == 1)
        {
            $data['consultant'] = CampusClewConsultantModel::field('id,truename as name')->select();
        }

        if(!empty($param['collector']) && $param['collector'] == 1)
        {
            $data['collector'] = ManystoreModel::field('id,username as name')->where('status','normal')->select();
        }

        if(!empty($param['teacher']) && $param['teacher'] == 1)
        {
            $data['teacher'] = ManystoreModel::field('id,username as name')->where(['status' => 'normal','level' => 2])->select();
        }

        if(!empty($param['distribution']) && $param['distribution'] == 1)
        {
            $where = ['status' => 'normal'];
            if($this->auth->level == 0)
            {
                $where['level'] = ['<>',0];
                $where['shop_id'] = $this->auth->shop_id;

                $data['distribution'] = ManystoreModel::field('id,username as name')->where($where)->select();
            }
            else if($this->auth->level == 1)
            {
                $where['level'] = 2;
                $where['shop_id'] = $this->auth->shop_id;

                $data['distribution'] = ManystoreModel::field('id,username as name')->where($where)->select();
            }
            else if($this->auth->level == 2)
            {
                $data['distribution'] = [];
            }
        }

        if(!empty($param['students_source']) && $param['students_source'] == 1)
        {
            $data['students_source'] = TypeModel::field('id,name')->where('type','studentsource')->select();
        }

        if(!empty($param['delete_why']) && $param['delete_why'] == 1)
        {
            $data['delete_why'] = TypeModel::field('id,name')->where('type','deletereason')->select();
        }

        if(!empty($param['growth']) && $param['growth'] == 1)
        {
            $data['growth'] = TypeModel::field('id,name')->where('type','growth')->select();
        }

        if(!empty($param['label']) && $param['label'] == 1)
        {
            $data['label'] = TypeModel::field('id,name')->where('type','labelmanage')->select();
        }

        if(!empty($param['clues']) && $param['clues'] == 1)
        {
            $data['clues'] = TypeModel::field('id,name')->where('type','clues')->select();
        }

        if(!empty($param['followway']) && $param['followway'] == 1)
        {
            $data['followway'] = TypeModel::field('id,name')->where('type','followway')->select();
        }

        if(!empty($param['followstage']) && $param['followstage'] == 1)
        {
            $data['followstage'] = TypeModel::field('id,name')->where('type','followstage')->select();
        }

        if(!empty($param['goodsubjects']) && $param['goodsubjects'] == 1)
        {
            $data['goodsubjects'] = TypeModel::field('id,name')->where('type','goodsubjects')->select();
        }

        if(!empty($param['quickentry']) && $param['quickentry'] == 1)
        {
            $data['quickentry'] = TypeModel::field('id,name')->where('type','quickentry')->select();
        }
        
        if(!empty($param['course']) && $param['course'] == 1)
        {
            $data['course'] = CourseModel::field('id,name')->where('status',1)->select();
        }

        if(!empty($param['student']) && $param['student'] == 1)
        {
            $where = ['student_type' => ['<>',3],'shop_id' => $this->auth->shop_id];
            if($this->auth->level == 2)
            {
                $where['follow_uid'] = $this->auth->id;
            }
            $data['student'] = UserModel::field('id,truename as name')->where($where)->select();
        }

        if(!empty($param['class']) && $param['class'] == 1)
        {
            $where = ['shop_id' => $this->auth->shop_id];
            if($param['class'] == 1)
            {
                $where['class_type'] = 2;
            }
            else if($param['class'] == 2)
            {
                $where['class_type'] = 1;
            }

            $data['class'] = CampusClass::field('id,name')->where('shop_id',$this->auth->shop_id)->select();
        }

        return $this->success('获取数据成功','',$data);
    }


}
